<template>
    <div>
        <el-card class="box-card" shadow="hover">
            <div slot="header" class="clearfix">
                <span>采购基本信息</span>
                <div style="float: right;">
                    <el-button size="small" @click="onClose">取 消</el-button>
                    <template v-if="rolesBtn.includes('purchaseRejectCode')">
                        <el-button size="small" type="danger" @click="allReject">驳回采购计划</el-button>
                    </template>
                    <template v-if="rolesBtn.includes('purchaseSubmitCode')">
                        <el-button size="small" type="primary" @click="onFormSubmit">保存采购计划</el-button>
                    </template>
                </div>
            </div>
            <!--驳回弹出框  选择驳回人员-->
            <el-dialog :title="node.title" :visible.sync="node.visible" :width="node.width" append-to-body :close-on-click-modal="false">
                <el-divider content-position="left">驳回节点</el-divider>
                <!--内勤-->
                <template v-if="row.jobStateId === 2">
                    <el-button size="mini" type="primary" @click="setUser(1)" icon="el-icon-s-custom">发起申请</el-button>
                </template>
                <!--分管领导-->
                <template v-if="row.jobStateId === 3">
                    <el-button size="mini" type="primary" @click="setUser(1)" icon="el-icon-s-custom">发起申请</el-button>
                    <el-button size="mini" type="primary" @click="setUser(2)" icon="el-icon-s-custom">内勤审核</el-button>
                </template>
                <!--总经理-->
                <template v-if="row.jobStateId === 4">
                    <el-button size="mini" type="primary" @click="setUser(1)" icon="el-icon-s-custom">发起申请</el-button>
                    <el-button size="mini" type="primary" @click="setUser(2)" icon="el-icon-s-custom">内勤审核</el-button>
                    <el-button size="mini" type="primary" @click="setUser(3)" icon="el-icon-s-custom">分管领导审核</el-button>
                </template>

                <el-divider content-position="left">点击节点，获取该节点人员，并分配驳回人员！</el-divider>
                <el-checkbox-group v-model="node.roleCheckbox" size="small">
                    <el-checkbox v-for="(items,index) in node.roleList" :key="index" :label="items.id" border>{{items.name}}</el-checkbox>
                </el-checkbox-group>
                <el-divider content-position="left">请输入驳回理由！</el-divider>
                <el-input type="textarea" :rows="2" placeholder="请输入驳回理由" v-model="node.suggestion"></el-input>
                <div slot="footer" class="dialog-footer">
                    <el-button size="mini" @click="node.visible  = false">取 消</el-button>
                    <el-button size="mini" type="primary" @click="onNodeSubmit">提交</el-button>
                </div>
            </el-dialog>
            <!--基础信息-->
            <table class="custom-table">
                <tr><td class="tr-title">申请人</td><td>{{this.row.applyUserName}}</td></tr>
                <tr><td class="tr-title">申请项目</td><td> {{this.row.name}}</td></tr>
                <tr><td class="tr-title">预计进场时间</td><td>{{this.row.startTime | formatLen}}</td></tr>
                <tr><td class="tr-title">申请时间</td><td>{{this.row.applyTime | formatLen}}</td></tr>
                <tr><td class="tr-title">使用位置</td><td> {{this.row.address}}</td></tr>
                <tr><td class="tr-title">备注</td><td> {{this.row.remarks}}</td></tr>
            </table>
        </el-card>


        <el-card class="box-card" shadow="hover">
            <div slot="header" class="clearfix">
              <span>物资信息</span>
              <template v-if="rolesBtn.includes('purchaseMaterialBatchRejectCode')"><!--权限包含才有-->
                <el-button style="float:right" type="danger" size="mini" @click="batchSingleRejectReject">批量驳回物资</el-button>
              </template>
            </div>
            <!--分类统计-->
            <table class="custom-table" style="margin-bottom:10px;">
                <tr v-for="item in material.materialInfo" :key="item.id">
                    <td class="tr-title">分类名称</td><td> {{item.type1Name}}</td>
                    <td class="tr-title">分类合计预估价</td><td> {{item.priceTotal}}</td>
                    <td v-if="index===0" :rowspan="material.materialInfo.length">预估合计：{{totalSumAll}}</td>
                </tr>
            </table>
            <el-alert v-if="tis" :title="tis" type="success" show-icon :closable="false"/>
            <!---物资列表-->
            <el-table :key="updateKey" :data="material.tableData" :row-key="row => { return row.id }" @selection-change="handleSelectionChange"  style="width: 100%;" ref="materialTable" stripe>
                <el-table-column fixed="left"  type="selection" width="55" :selectable="row=>{return row.rejectFlag !== '1'}" :reserve-selection="true"></el-table-column>
                <el-table-column prop="goodsName" label="物资名称" width="140"></el-table-column>
                <el-table-column prop="type1Name" label="分类" width="120"></el-table-column>
                <el-table-column prop="unit" label="单位" width="70"></el-table-column>
                <el-table-column prop="applyNum" label="申请数量" width="140">
                    <template slot-scope="scope">
                        <el-input-number v-model="scope.row.applyNum" :disabled="scope.row.rejectFlag === '1'" @change="function(val){return inputPrice(val,scope.row,1)}" :step="1" step-strictly size="mini" :min="1" :max="100000"></el-input-number>
                    </template>
                </el-table-column>
                <el-table-column prop="estimatePrice" label="预估价" width="140">
                </el-table-column>
                <el-table-column prop="purchaseType" label="采购方式" width="150">
                    <template slot-scope="scope">
                       <el-select v-model="scope.row.purchaseType" :disabled="scope.row.rejectFlag === '1'" @change="function(val){return purchaseTypeChange(val,scope.row)}">
                            <el-option v-for="item in material.purchaseTypeData" :key="item.value" :label="item.label" :value="item.value"></el-option>
                       </el-select>
                    </template>
                </el-table-column>
                <el-table-column prop="skuInfo" label="技术参数及规格要求">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.skuInfo" :disabled="scope.row.rejectFlag === '1'" type="textarea"></el-input>
                    </template>
                </el-table-column>
                <el-table-column prop="rejectFlag" label="状态" width="80" fixed="right">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.rejectFlag === '0'">未驳回</el-tag>
                        <el-tag v-if="scope.row.rejectFlag === '1'" type="warning">已驳回</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="160">
                    <template slot-scope="scope">
                        <el-button type="primary" v-if="scope.row.rejectFlag === '1'" @click="editSingleReject(scope.row)" size="mini">修改</el-button>
                        <template v-if="scope.row.purchaseRejectFlag">
                            <el-button type="warning" v-if="scope.row.rejectFlag === '0'" @click="singleReject(scope.row)" size="mini">驳回</el-button>
                        </template>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>


        <!--明细 从分管领导开始 状态3-->
        <el-card class="box-card" shadow="hover" v-if="row.jobStateId ===3 || row.jobStateId ===4">
            <div slot="header" class="clearfix">
              <span>审批明细</span>
            </div>
            <el-table :data="node.tableData" style="width: 100%;" ref="table3" stripe>
                <el-table-column prop="updateUserName" label="办理人" width="120"></el-table-column>
                <el-table-column prop="stepName" label="办理步骤" width="180"></el-table-column>
                <el-table-column prop="updateTime" label="办理时间" width="180" :formatter="formatDate"></el-table-column>
                <el-table-column prop="changeInfo" label="办理意见"></el-table-column>
            </el-table>
        </el-card>
        
         <!--内勤审核-->
         <el-card class="box-card" shadow="hover" v-if="row.jobStateId ===2">
            <div slot="header" class="clearfix">
                <span>分配转交人</span>
                <el-button style="float: right;" size="small" type="primary" @click="workflowOpen">选择转交人员</el-button>
            </div>
            <el-dialog :title="workflow.title" :visible.sync="workflow.visible" :width="workflow.width" append-to-body :close-on-click-modal="false">
                <el-radio-group v-model="workflow.applyUserId">
                    <el-radio v-for="item in workflow.workflowList" :border="true" :key="item.id" :label="item.id">
                    {{item.name}}
                    </el-radio>
                </el-radio-group>
                <div slot="footer" class="dialog-footer">
                <el-button size="small" @click="workflow.visible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="submitWorkflow">保存</el-button>
                </div>
            </el-dialog>
            <table class="custom-table">
                <tr><td class="tr-title">转交步骤</td><td>分管领导审核</td></tr>
                <tr><td class="tr-title">转交人员</td><td>{{workflow.applyUser}}</td></tr>
                <tr><td class="tr-title">办理意见</td><td><el-input type="textarea" v-model="workflow.remarks"/></td></tr>
            </table>
        </el-card>

        <!--分管领导审核-->
        <el-card class="box-card" shadow="hover" v-if="row.jobStateId ===3">
            <div slot="header" class="clearfix">
                <span>分配转交人</span>
                <el-button style="float: right;" size="small" v-if="totalMoneyFlag!==2" type="primary" @click="workflowOpen">选择转交人员</el-button>
            </div>
            <el-dialog :title="workflow.title" :visible.sync="workflow.visible" :width="workflow.width" append-to-body :close-on-click-modal="false">
                <el-radio-group v-model="workflow.applyUserId">
                    <el-radio v-for="item in workflow.workflowList" :border="true" :key="item.id" :label="item.id">
                    {{item.name}}
                    </el-radio>
                </el-radio-group>
                <div slot="footer" class="dialog-footer">
                <el-button size="small" @click="workflow.visible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="submitWorkflow">保存</el-button>
                </div>
            </el-dialog>
            <table class="custom-table">
                <tr><td class="tr-title">是否总经理审核</td><td>
                    <el-radio-group v-model="totalMoneyFlag">
                        <el-radio :label="1">是</el-radio>
                        <el-radio :label="2">否</el-radio>
                      </el-radio-group>
                </td></tr>
                <tr><td class="tr-title">转交步骤</td><td>分管领导审核</td></tr>
                <tr v-if="totalMoneyFlag!==2"><td class="tr-title">转交人员</td><td>{{workflow.applyUser}}</td></tr>
                <tr><td class="tr-title">办理意见</td><td><el-input type="textarea" v-model="workflow.remarks"/></td></tr>
            </table>
        </el-card>
       

        <!--  总经理   -->
        <template v-if="row.jobStateId ===4">
            <el-card class="box-card" shadow="hover">
                <div slot="header" class="clearfix">
                    <span>审核意见</span>
                </div>
                <table class="custom-table">
                    <tr><td class="tr-title">办理意见</td><td><el-input type="textarea" v-model="workflow.remarks"/></td></tr>
                </table>
            </el-card>
        </template>
       
    </div>
</template>

<script>
    import moment from 'moment'
    import {PURCHASE_TYPE_DATA} from "@/config/common"
    import { 
        purchaseApplyMaterial,
        purchaseSaveMaterial,
        purchaseApplyUser,
        purchaseMaterialRejectBatch,
        purchaseReject,
        purchaseSuccess,
        purchaseApplyLog
    } from "@/api/purchase_api"
    export default {
        props: ['row'],
        data() {
            return {
                material:{
                    tableData:[],
                    materialInfo:[],
                    purchaseTypeData:PURCHASE_TYPE_DATA
                },
                workflow:{
                    title:'选择审核人',
                    width:"600px",
                    visible:false,
                    applyUserId:'',
                    applyUser:'',
                    remarks:'',
                    workflowList:[]
                },
                params: {
                    type:"page",
                    pageNum: 1,
                    pageSize: 10
                },
                updateKey:0,//驳回操作后更新table组件
                //驳回配置,和审批明细
                node:{
                    id:'',
                    roleCheckbox:[],//节点人员
                    suggestion:'',//意见
                    roleList:[],//人员列表
                    toStateId:'',//当前驳回节点id
                    title:'驳回节点和人员配置',
                    width:"600px",
                    visible:false,
                    tableData:[]
                },
                totalMoneyFlag:'',//是否总经理审核
                selecedItem:[],//物资批量驳回
                tis:''//选中提示
            };
        },
        filters: {
            formatLen(value) {
                if (!value) {
                    return ''
                }
                return moment(value).format("YYYY-MM-DD  HH:mm:ss")
            }
        },
        computed: {
            rolesBtn() {
                return this.$store.getters.permission_user.roles.length >=1 ? this.$store.getters.permission_user.roles[0].permissions:[];
            },
            totalSumAll(){//物资分类评估价合计
                let totalSumAll = 0;
                this.material.materialInfo.map((item) => {
                    totalSumAll += parseFloat(item.priceTotal)
                })

                if((totalSumAll+'').indexOf(".") !== -1){
                   let str = totalSumAll.toString().split('.');
                   totalSumAll = str[1].length === 1 ? totalSumAll.toFixed(1) : totalSumAll.toFixed(2);
                }
                return totalSumAll;
            }
        },
        mounted(){
            this.getPurchaseDetails(this.row.id);
            if(this.row.jobStateId === 3 || this.row.jobStateId === 4){//分管领导 总经理才调用明细
               this.getPurchaseLog(this.row.id);
            }
            
        },
        methods: {
            formatDate(row, column) {
                const date = row[column.property]
                if (date == undefined) {
                return ''
                }
                return moment(date).format("YYYY-MM-DD  HH:mm:ss")
            },
            //获取批量明细
            async getPurchaseLog(id){
                const {code,data} = await purchaseApplyLog(id);
                if(code === 200){
                    this.node.tableData = data;
                }
            },
            //查询物资
            async getPurchaseDetails(id){
               
                const {code,data} = await purchaseApplyMaterial(id);
                if(code === 200){
                     //初次加载 需要转换 整数/100
                     data.forEach(v=>{
                        v.estimatePrice = (v.estimatePrice/100)
                    })
                    this.material.tableData = data;
                   
                    let btn = this.$store.getters.permission_btns;
                    this.material.tableData.forEach(v=>{
                        if(btn.includes('purchaseRejectFlag')){//驳回按钮
                            v.purchaseRejectFlag = true;
                        }
                    })
                   
                    this.materialClassifyTotal();
                    
                }
            },
            //修改物资数据
            async purchaseTypeChange(val,row){
                if(val){
                    row.purchaseType=val;
                }
                await purchaseSaveMaterial(row);
            },
            //物资计算统计
            inputPrice(val,row,type){
                if(!val){//未空的不管
                    return;
                }
                //只能为数字 小数点两位
                if(!/^(?!0+(?:\.0+)?$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$/.test(val)){
                    return;
                }
                //执行物资更新操作
                this.purchaseTypeChange('',row);
                this.materialClassifyTotal();
            },
            materialClassifyTotal(){
                let that = this;
                let countMap = {};
                that.material.tableData.forEach(item=>{
                    if(!countMap[item.goodTypeId1]){
                        countMap[item.goodTypeId1] = {
                            priceTotal:0,
                            type1Name:item.type1Name
                        }
                    }
                    if(item.applyNum !== undefined && item.estimatePrice !== undefined && item.rejectFlag === '0'){//两个值都有
                        let price = item.estimatePrice * item.applyNum;
                        let prices = parseFloat(countMap[item.goodTypeId1].priceTotal) + parseFloat(price);
                        if((prices+'').indexOf(".") !== -1){
                            let str = prices.toString().split('.');
                            countMap[item.goodTypeId1].priceTotal = str[1].length === 1 ? prices.toFixed(1) : prices.toFixed(2);
                        }else{
                            countMap[item.goodTypeId1].priceTotal = prices
                        }
                        //countMap[item.goodTypeId1].priceTotal = (prices+'').indexOf(".") !== -1 ? prices.toFixed(2) : prices;
                    }
                    let materialInfo = [];
                    for(var p in countMap){
                        materialInfo.push(countMap[p]);
                    }
                    that.material.materialInfo = materialInfo;
                    
                })
            },
 /*-----------------------------------------------------------驳回物资操作----------------------------------------------------------------------------------------------*/
            //复选框
            handleSelectionChange(val){
                let ids = [];
                val.forEach(item =>{ 
                    ids.push(item)
                });
                this.selecedItem = ids;
                this.tis="已选择 "+val.length+" 项";
                if(val.length==0){
                    this.tis="";
                    this.selecedItem = [];
                }
            },
            //批量驳回
            batchSingleRejectReject(){
                if(this.selecedItem.length === 0){
                    return this.$message.error('请选择驳回物资！');
                }
                this.$prompt('请输入驳回理由', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputType:'textarea',
                inputValidator(value){
                    if (value == undefined) {
                        return '驳回理由不能为空'
                    } else if (value.length == 0) {
                        return '驳回理由不能为空'
                    } 
                },
                }).then(async({ value }) => {
                    try {
                        let _ids = this.selecedItem.map(v=>v.id);
                        let params = {};
                        params.ids = this.selecedItem.map(v=>v.id);
                        params.reason = value;
                        const {code,msg} = await purchaseMaterialRejectBatch(params);
                        if(code === 200){
                            this.$message.success('操作成功！');
                            this.clearCheck();
                            this.getPurchaseDetails(this.row.id);//重新调用物资接口
                        }
                    } catch (error) {
                        this.$message.info(JSON.stringify(error));
                    }
                }).catch(() => {});
               /*  this.$confirm("确认驳回", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
                }).then(async () => {
                    try {
                        let _ids = this.selecedItem.map(v=>v.id);
                        const {code,msg} = await purchaseMaterialRejectBatch(_ids);
                        if(code === 200){
                            this.$message.success('操作成功！');
                            this.clearCheck();
                            this.getPurchaseDetails(this.row.id);//重新调用物资接口
                        }

                    } catch (error) {
                        this.$message.info(JSON.stringify(error));
                    }
                }).catch(() => {}); */
            },
            clearCheck(){
                this.$refs.materialTable.clearSelection();
                this.selecedItem = [];
                this.tis = "";
            },
            //修改驳回状态
            editSingleReject(row){
                this.$confirm('确认修改已驳回为未驳回', "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
                }).then(async () => {
                    try {
                        row.rejectFlag = '0';
                        const {code,msg} = await purchaseSaveMaterial(row);
                        if(code === 200){
                            this.updateKey+=1;//解决操作列表时 不能更新数据问题
                            this.$message.success('操作成功！');
                            this.getPurchaseDetails(this.row.id);//重新调用物资接口
                        }
                    } catch (error) {
                        this.$message.info(JSON.stringify(error));
                    }
                }).catch(() => {});
            },
            //驳回物资
            singleReject(row){
                this.$prompt('请输入驳回理由', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputType:'textarea',
                inputValidator(value){
                    if (value == undefined) {
                        return '驳回理由不能为空'
                    } else if (value.length == 0) {
                        return '驳回理由不能为空'
                    } 
                },
                }).then(async({ value }) => {
                    try {
                        let _ids = this.selecedItem.map(v=>v.id);
                        let params = {};
                        params.ids = [row.id];
                        params.reason = value;
                        const {code,msg} = await purchaseMaterialRejectBatch(params);
                        if(code === 200){
                            this.$message.success('操作成功！');
                            this.clearCheck();
                            this.getPurchaseDetails(this.row.id);//重新调用物资接口
                        }
                    } catch (error) {
                        this.$message.info(JSON.stringify(error));
                    }
                }).catch(() => {});
                /* this.$confirm('确认驳回', "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
                }).then(async () => {
                    try {
                        let _ids = [row.id];
                        const {code,msg} = await purchaseMaterialRejectBatch(_ids);
                        if(code === 200){
                            this.$message.success('操作成功！');
                            this.getPurchaseDetails(this.row.id);//重新调用物资接口
                        }
                    } catch (error) {
                        this.$message.info(JSON.stringify(error));
                    }
                }).catch(() => {}); */
            },
//-----------------------------------------------------------------转交人相关操作------------------------------------------------------------------------
            async workflowOpen(){
                const jobStateId = this.row.jobStateId + 1;
                const { code,data,msg } = await purchaseApplyUser(jobStateId);
                if(code === 200){
                    this.workflow.applyUserId = '';
                    this.workflow.workflowList = data;
                    this.workflow.visible = true;
                }
            },
            submitWorkflow(){
                if(!this.workflow.applyUserId){
                    return this.$message.error('请选择转交人员！');
                }
                //匹配处理人
                let user = this.workflow.workflowList.find(v=>v.id === this.workflow.applyUserId);
                this.workflow.applyUser = user.name;
                this.workflow.visible = false;
            },
 //---------------------------------------------------------------提交-----------------------------------------------------------------
            onFormSubmit() {
                const arr = this.material.tableData;
                for(var i=0;i<arr.length;i++){
                    if(arr[i].rejectFlag === '0'){//只检测未驳回的
                        if(arr[i].applyNum === undefined || arr[i].applyNum === ''){
                            return this.$message.error('检测到物资第'+(i+1)+'行申请数量未填写！');
                        }
                        if(arr[i].estimatePrice === undefined || arr[i].estimatePrice === ''){
                            return this.$message.error('检测到物资第'+(i+1)+'行预估价未填写！');
                        }
                        if(arr[i].purchaseType === undefined || arr[i].purchaseType === ''){
                            return this.$message.error('检测到物资第'+(i+1)+'行申采购方式未选择！');
                        }
                    }
                }

                if(this.row.jobStateId === 3){
                    if(this.totalMoneyFlag ===1){//1 是  需要选择转交人
                        if(!this.workflow.applyUser){
                            return this.$message.error('请选择转交人员');
                        }
                    }
                }else if(this.row.jobStateId === 2){
                    if(!this.workflow.applyUser){
                        return this.$message.error('请选择转交人员');
                    }
                }

                
                if(!this.workflow.remarks){
                    return this.$message.error('请填写审核意见！');
                }

                let text = '';
                if(this.row.jobStateId === 3){
                     //为true 需要总经理审核 为false 就不需要
                     text = this.totalMoneyFlag ===1 ? '确认提交！':'提交后，该物资采购计划进入物资招标发起阶段，确认提交';
                }else if(this.row.jobStateId === 4){
                    text = '提交后，该物资采购计划进入物资招标发起阶段，确认提交';
                }else{
                     text = '确认提交';
                }

                
                this.$confirm(text, "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
                }).then(async () => {
                    try {
                        /*
                        fromStateId	当前步骤ID
                        handleUser	处理人ID	
                        planId	采购计划ID		
                        stepName	当前步骤名称
                        suggestion	办理意见	
                        toStateId	变更到状态ID	

                        jobStateId==1 已驳回
                        jobStateId==2 内勤审核
                        jobStateId==3 分管领导审核
                        jobStateId==4 总经理审核
                        jobStateId==5 已成功
                        jobStateId==6 已终止

                        */

                        let jobStateId = '';
                        let stepName = '';

                      
                        if(this.row.jobStateId === 2){
                            jobStateId = 3;
                            stepName = '内勤审核';
                        } else if(this.row.jobStateId === 3){
                            //如果 2  直接审核结束
                            if(this.totalMoneyFlag ===2){
                                jobStateId = 5;
                                stepName = '分管领导审核';
                            }else{//如果大于20w 需要总经理审核
                                jobStateId = 4;
                                stepName = '分管领导审核';
                            }
                        }else if(this.row.jobStateId === 4){
                            jobStateId = 5;
                            stepName = '总经理审核';
                        }

                        let params = {
                            fromStateId:this.row.jobStateId,
                            handleUser:this.workflow.applyUserId,
                            planId:this.row.id,
                            stepName:stepName,
                            suggestion:this.workflow.remarks,
                            toStateId:jobStateId,
                        }
                        const {code,msg} = await purchaseSuccess(params);
                        if(code === 200){
                            this.$message.success('操作成功！');
                            this.onClose();//关闭弹出框
                            this.loadList();//驳回后刷新列表
                        }
                    } catch (error) {
                        this.$message.info(JSON.stringify(error));
                    }
                }).catch(() => {});
            },
            //---------------------------------------------------驳回--------------------------------------------------------------------------------
            async allReject(){
                const {code,data} = await purchaseApplyUser(1);
                if(code === 200){
                    this.toStateId = 1;//默认申请人节点
                    this.node.roleCheckbox =[];
                    this.node.roleList = [];
                    this.node.roleList = data;
                    this.node.visible = true;
                }
            },
            async setUser(stateId){
                const {code,data} = await purchaseApplyUser(stateId);
                if(code === 200){
                    this.toStateId = stateId;//根据这个参数 判断驳回到哪个节点
                    this.node.roleList = [];
                    this.node.roleList = data;
                }
            },
            onNodeSubmit(){
                if(this.node.roleCheckbox.length === 0){
                    return this.$message.error('请选择驳回人员！');
                }
                if(!this.node.suggestion){
                    return this.$message.error('请填写驳回理由！');
                }
                 this.$confirm("确认驳回", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
                }).then(async () => {
                    try {
                        /*
                        fromStateId	当前步骤ID
                        handleUser	处理人ID	
                        planId	采购计划ID		
                        stepName	当前步骤名称
                        suggestion	办理意见	
                        toStateId	变更到状态ID	
                        */
                        /* 
                            jobStateId==1 已驳回
                            jobStateId==2 内勤审核
                            jobStateId==3 分管领导审核
                            jobStateId==4 总经理审核
                            jobStateId==5 已成功
                            jobStateId==6 已终止
                        */
                       //这里需要时当前状态名称
                       let jobStateName = '';
                        if(this.row.jobStateId === 2){
                            jobStateName = '内勤审核';
                        } else if(this.row.jobStateId === 3){
                            jobStateName = '分管领导审核';
                        }else if(this.row.jobStateId === 4){
                            jobStateName = '总经理审核';
                        }
                        const params = {
                            fromStateId:this.row.jobStateId,
                            handleUser:this.node.roleCheckbox.join(','),
                            planId:this.row.id,
                            suggestion:this.node.suggestion,
                            stepName:jobStateName,
                            toStateId:this.toStateId,
                        }
                        const {code,msg} = await purchaseReject(params);
                        if(code === 200){
                            this.$message.success('操作成功！');
                            this.node.visible = false;//关闭当前弹出框
                            this.onClose();//关闭弹出框
                            this.loadList();//驳回后刷新列表
                        }
                    } catch (error) {
                        this.$message.info(JSON.stringify(error));
                    }
                }).catch(() => {});
            },
            loadList() {
                this.$emit("on-search-click", this.params);
            },
            onClose() {
                this.$emit("on-cancel-click","initVerifyReject");
            }
        }
    };
    </script>
    
    